package behaviors;

import math.matrix.Matmath;
import math.matrix.mat4f;
import math.vector.Vecmath;
import math.vector.vec2f;
import math.vector.vec3f;

public class ViewMatrices
{
    
    private mat4f matView;
    //<editor-fold defaultstate="collapsed" desc="getMatrixView">
    public mat4f getMatrixView()
    {
        return this.matView;
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="setMatrixView">
    public void setMatrixView(mat4f matrixView)
    {
        this.matView.setTo(matrixView);
    }
    
    public void setMatrixViewTo(vec3f viewPosition, vec2f viewRotations)
    {
        vec3f viewTrans3D = Vecmath.getInvertedOf(viewPosition);
        vec3f viewRots3D = new vec3f(viewRotations.x, viewRotations.y, 0.0f);
        mat4f matTranslation = this.genMatrixTranslation(viewTrans3D);
        mat4f matRotation = this.genMatrixRotation(viewRots3D);
        Matmath.multiply( // Set VIEW to ROT * TRANS
            matRotation, matTranslation, // ROT * TRANS
            this.matView // Matrix destination
        );
    }
    //</editor-fold>
    
    private mat4f matProj;
    //<editor-fold defaultstate="collapsed" desc="getMatrixProj">
    public mat4f getMatrixProj()
    {
        return this.matProj;
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="setMatrixProj">
    public void setMatrixProj(mat4f matrixProj)
    {
        this.matProj.setTo(matrixProj);
    }
    
    public void setMatrixProjTo2D(int width, int height)
    {
        Matmath.calcOrtho(
            0, width,  // xl, xr
            0, height, // yl, yr
            -1, 1,     // zl, zr
            this.matProj // destination matrix
        );
    }
    
    public void setMatrixProjTo3D(
            float fovYDeg, 
            float aspectRatio, 
            float zNear, 
            float zFar)
    {
        Matmath.calcProjection(
            fovYDeg,
            aspectRatio,
            zNear,
            zFar,
            this.matProj // destination matrix
        );
    }
    //</editor-fold>
    
    public ViewMatrices()
    {
        this.matView = new mat4f();
        this.matProj = new mat4f();
    }
    
    private mat4f genMatrixRotation(vec3f rotations)
    {
        mat4f matRotation = new mat4f();
        Matmath.calcRotationMatXYZ( // Set up ROT matrix
            rotations.x, rotations.y, rotations.z, // Rotation vector
            matRotation // Matrix destination
        );
        return matRotation;
    }
    
    private mat4f genMatrixTranslation(vec3f translation)
    {
        mat4f matViewTrans = new mat4f();
        Matmath.calcTranslation( // Set up TRANS matrix
            translation.x, translation.y, translation.z,  // Translation vector
            matViewTrans // Matrix destination
        );
        return matViewTrans;
    }
    
    public void destroy()
    {
        this.matView = null;
        this.matProj = null;
    }
    
}
